<!DOCTYPE html>
<html>
<head>
  <script src="../../js-test-resources/js-test.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description('Tests that popstate events fire when going back, even when there are resource requests pending.');

window.onload = function()
{
    debug('Starting slow image request');

    var slowImage = document.createElement('img');
    slowImage.src = 'resources/slow-image.php';
    document.body.appendChild(slowImage);

    debug('Pushing newState1');
    history.pushState('newState1', null, '?newState1');

    setTimeout(function() {
        debug('Current search: ' + location.search);

        debug('Pushing newState2');
        history.pushState('newState2', null, '?newState2');
        setTimeout(function() {
            debug('Current search: ' + location.search);
            debug('Going back');
            history.back();
        }, 0);
    }, 0);
}

window.onpopstate = function(event)
{
    shouldBe('event.state', '"newState1"');
    // Stop the pending image request, otherwise the DRT doesn't consider the
    // test over.
    window.stop();
    finishJSTest();
}

var jsTestIsAsync = true;
</script>

</body>
</html>
